Multithread (namespace Mt)


BoolTs: bool Thread Safe
BoolTs(const Mt::BoolTs& init)
BoolTs(void)
Mt::BoolTs& operator =(const bool init)
bool CheckThenSet(bool check_value, bool set_value)
If the stored value is the same as check_value, it returns true and sets the stored value to set_value If the stored value is not the same as check_value, it returns false
bool Get(void)const
bool operator !=(const bool init)const
bool operator ==(const bool init)const
void Set(bool value)
~ BoolTs(void)

CriticalSection
CriticalSection()
bool Try()
virtual ~ CriticalSection()
void Enter()
void Leave()

DecimalTs: decimal Thread Safe
DecimalTs(const Mt::DecimalTs& init)
DecimalTs(double init)
DecimalTs(void)
Mt::DecimalTs& operator =(const Mt::DecimalTs& init)
Mt::DecimalTs& operator =(const double init)
bool operator !=(const Mt::DecimalTs& init)const
bool operator !=(const double init)const
bool operator ==(const Mt::DecimalTs& init)const
bool operator ==(const double init)const
double Get(void)const
operator const double() const
operator double() const
void Set(double value)
~ DecimalTs(void)

Deque
Deque(void)
int Extract()
int Extract(char* data, int max)
int GetSpaceFree() const
int GetSpaceUsed() const
int Insert(char c)
int Insert(char* data, int count)
int Peek(char* data, int max)const
void Clear()
~ Deque(void)

DoubleTs: double Thread Safe
DoubleTs(void)
Mt::DoubleTs& operator =(const double init)
bool GetTry(double& value)const
bool SetTry(double value)
bool operator !=(const double init)const
bool operator ==(const double init)const
double Get(void)const
double operator =(const Mt::DoubleTs& init)
operator double() const
void Set(double value)
~ DoubleTs(void)

EventI: An event, use for multithreading, by the default the event is a non-signaled autoreset event
DWORD Wait()
It waits until the event is in the signaled state  Google Microsoft
DWORD Wait(DWORD milliseconds)
It waits until the event is in the signaled state or the time-out interval elapses  Google Microsoft
EventI()
HANDLE Create(LPSECURITY_ATTRIBUTES eventAttributes, bool isManualReset, bool initiallySignaled, const wchar_t* name)
It creates or opens a named or unnamed event object  Google Microsoft
HANDLE GetHandle(void)
bool CreateAutoReset(bool initiallySignaled)
The system automatically resets the event state to nonsignaled after a single waiting thread has been released  Google Microsoft
bool CreateManualReset(bool initiallySignaled)
It requires the use of the event.Reset function to set the event state to nonsignaled  Google Microsoft
bool Open(DWORD dwDesiredAccess, bool inheritHandle, const wchar_t* name)
It enables multiple processes to open handles of the same event object. The function succeeds only if some process has already created the event by using the event.CreateEvent function  Google Microsoft
bool Reset()
It sets the state of the event to nonsignaled until explicitly set to signaled by the event.Set member function. This causes all threads wishing to access this event to wait. This function is ONLY used for manual-reset events
bool Set()
It sets the state of the event to signaled, releasing any waiting threads If the event is manual, the event will remain signaled until event.Reset is called. More than one thread can be released in this case. If the event is automatic, the event will remain signaled until a single thread is released. The system will then set the state of the event to nonsignaled. If no threads are waiting, the state remains signaled until one thread is released  Google Microsoft
operator HANDLE() const
operator const HANDLE() const
~ EventI()

Gate
BOOL Open()
DWORD WaitToOpen(DWORD dwTimeout = INFINITE, BOOL fAlertable = FALSE)
Gate()
Gate(BOOL fInitiallyOpen)
Gate(BOOL fInitiallyOpen, PCTSTR pszName)
~ Gate()

IOCport: It provides support to use an Input Output Completion Port. An IOCport limits the number of threads that the system allows running concurrently (one thread per processor in the system)
# endif bool PostStatus(ULONG_PTR compKey)
It posts an I/O completion packet to an I/O completion port. This function simulates an I/O operation completion to unblock a called to GetStatus  Google Microsoft
# ifdef WIN_SOCKETS_SUPPORT bool AssociateSocket(SOCKET hSocket, ULONG_PTR compKey)
It associates an I/O completion port with a socket  Google Microsoft
IOCport()
IOCport(int maxConcurrency)
bool AssociateDevice(HANDLE hDevice, ULONG_PTR compKey)
It associates an I/O completion port with a handle  Google Microsoft
bool Create(int maxConcurrency)
You do not need to call this function, unless you need to change maxConcurrency  Google Microsoft
bool GetStatus(ULONG_PTR& compKey)
It attempts to dequeue an I/O completion packet from the specified I/O completion port. If there is no completion packet queued, the function waits for a pending I/O operation associated with the completion port to complete. This function is similar to ::WaitForMultipleObjects compKey: pointer to a variable that receives the completion key value associated with the file handle whose I/O operation has completed.  Google Microsoft
bool GetStatus(ULONG_PTR& compKey, DWORD& numBytes)
It attempts to dequeue an I/O completion packet from the specified I/O completion port. If there is no completion packet queued, the function waits for a pending I/O operation associated with the completion port to complete. This function is similar to ::WaitForMultipleObjects compKey: pointer to a variable that receives the completion key value associated with the file handle whose I/O operation has completed. numBytes: pointer to a variable that receives the number of bytes transferred during an I/O operation that has completed  Google Microsoft
bool GetStatus(ULONG_PTR& compKey, DWORD& numBytes, OVERLAPPED* * ppOverlapped)
It attempts to dequeue an I/O completion packet from the specified I/O completion port. If there is no completion packet queued, the function waits for a pending I/O operation associated with the completion port to complete. This function is similar to ::WaitForMultipleObjects compKey: pointer to a variable that receives the completion key value associated with the file handle whose I/O operation has completed. numBytes: pointer to a variable that receives the number of bytes transferred during an I/O operation that has completed ppOverlapped:: pointer to a variable that receives the address of the OVERLAPPED structure that was specified when the completed I/O operation was started.  Google Microsoft
bool GetStatus(ULONG_PTR& compKey, DWORD& numBytes, OVERLAPPED* * ppOverlapped, DWORD milliseconds)
It attempts to dequeue an I/O completion packet from the specified I/O completion port. If there is no completion packet queued, the function waits for a pending I/O operation associated with the completion port to complete. This function is similar to ::WaitForMultipleObjects compKey: pointer to a variable that receives the completion key value associated with the file handle whose I/O operation has completed. numBytes: pointer to a variable that receives the number of bytes transferred during an I/O operation that has completed ppOverlapped:: pointer to a variable that receives the address of the OVERLAPPED structure that was specified when the completed I/O operation was started. milliseconds: the time that the caller is willing to wait for a completion packet to appear at the completion port.If a completion packet does not appear within the specified time  Google Microsoft
bool PostStatus(ULONG_PTR compKey, DWORD numBytes)
It posts an I/O completion packet to an I/O completion port. This function simulates an I/O operation completion to unblock a called to GetStatus  Google Microsoft
bool PostStatus(ULONG_PTR compKey, DWORD numBytes, OVERLAPPED& overlapped)
It posts an I/O completion packet to an I/O completion port. This function simulates an I/O operation completion to unblock a called to GetStatus  Google Microsoft
~ IOCport()

IOCportTimer: It provides support to use an Input Output Completion Port Timer
IOCportTimer(void)
bool Start(Mt::IOCport* iocp, ULONG_PTR completionKey, int iniMillisec, int repeatMillisec)
static void DisplayLastError(const wchar_t* pszWhere, bool bServiceNotification)
virtual ~ IOCportTimer(void)
void Stop()

IThread
virtual DWORD ThreadFunc(Mt::ThreadLink& threadLink)= 0
Write your thread code inside this function. threadLink.Cancel: you should return as quickly as possible from ThreadFunc when this property is true threadLink.Progress: use this property to report the progress of the thread (0.0 to 100.0) threadLink.ResetTime: use this variable when the thread has several steps and you want to report progress for each step. (if your thread does not have any steps, you can ignore this variable). Set "ResetTime" to true, at the beginning of each step

IThreadX
virtual DWORD ThreadFunc(Mt::BoolTs& cancel, size_t threadIndex, size_t numThreads)= 0
Write your thread code inside this function. When cancel is true, you should return as quickly as possible from ThreadFunc threadIndex: 0, 1, 2, 3, ... numThreads-1

InputOutputRequest
InputOutputRequest()
bool AllocBuffer(SIZE_T bufferSize)
bool Read(HANDLE hDevice)
bool Read(HANDLE hDevice, LARGE_INTEGER offset)
bool Write(HANDLE hDevice)
bool Write(HANDLE hDevice, LARGE_INTEGER offset)
~ InputOutputRequest()

InputOutputRequestX
InputOutputRequestX()
bool AllocBuffer(SIZE_T bufferSize)
bool ReadA(HANDLE hDevice, LARGE_INTEGER offset)
bool ReadB(HANDLE hDevice, LARGE_INTEGER offset)
int CompareAandB()
~ InputOutputRequestX()

IntTs: int Thread Safe
IntTs()
Mt::IntTs& operator =(const int init)
bool GetTry(int& value)
bool SetTry(int value)
bool operator ==(const int init)
int Get()
int operator =(const Mt::IntTs& init)
void Set(int value)
~ IntTs()

LongTs: long Thread Safe
LONG Add(LONG increment)
LONG Get(void)const
LONG Set(LONG value)
LongTs(LONG value)
LongTs(const Mt::LongTs& init)
LongTs(void)
Mt::LongTs& operator =(const LONG init)
Mt::LongTs& operator =(const Mt::LongTs& init)
bool operator ==(const LONG init)const
bool operator ==(const Mt::LongTs& init)const
operator LONG()
operator LONG() const
operator const LONG() const
~ LongTs(void)

Mutex: MUTual EXclusion: only one thread (or process) at a time is allowed to own a mutex mutexex can be used between threads and processes
DWORD Wait()
It waits until the Mutex is in the signaled state (This is similar to ::EnterCriticalSection)  Google Microsoft
DWORD Wait(DWORD milliseconds)
It waits until the Mutex is in the signaled state or the time-out interval elapses (This is similar to ::EnterCriticalSection)  Google Microsoft
HANDLE Create(LPSECURITY_ATTRIBUTES& security_attributes, bool initialOwner, const wchar_t* name)
It returns a handle if successful, otherwise it returns nullptr
HANDLE Create(bool initialOwner, const wchar_t* name)
It returns a handle if successful, otherwise it returns nullptr
HANDLE GetHandle()
HANDLE Open(DWORD desiredAccess, bool inheritHandle, const wchar_t* name)
Open an existing mutex with a specific name than has been created by any other thread or process It returns a handle if successful, otherwise it returns nullptr
Mutex()
bool Release()
It release the mutex (this is similar to ::LeaveCritialSection)
operator HANDLE() const
operator const HANDLE() const
virtual ~ Mutex()
void Delete()

OverlappedE: It provides support for asynchronous programming in multithread applicacions
HANDLE GetEventHandle(void)
OverlappedE(void)
void ResetEvent(void)
~ OverlappedE(void)

Semaphore: It allows to share simultaneously a resource by a maximum number of threads (or processes)
DWORD Wait()
It decrements the semaphore count When you call this function a maximumCount number of times, it will not block (semaphore count is not zero) When you can this function more times than maximumCount, it will block until Release is called (semaphore count is zero)  Google Microsoft
DWORD Wait(DWORD milliseconds)
It decrements the semaphore count When you call this function a maximumCount number of times, it will not block (semaphore count is not zero) When you can this function more times than maximumCount, it will block until Release is called (semaphore count is zero)  Google Microsoft
HANDLE Create(LONG initialCount, LONG maximumCount)
It returns a handle if successful, otherwise it returns nullptr
HANDLE Create(LPSECURITY_ATTRIBUTES& security_attributes, LONG initialCount, LONG maximumCount, const wchar_t* name)
It returns a handle if successful, otherwise it returns nullptr
HANDLE GetHandle()
HANDLE Open(DWORD desiredAccess, bool inheritHandle, const wchar_t* name)
It opens an existing named semaphore object
Semaphore()
bool Release(LONG releaseCount, LONG& out_previousCount)
It releases the semaphore (it increments the semaphore count by the number specified in releaseCount)
operator HANDLE() const
operator const HANDLE() const
virtual ~ Semaphore()
void Delete()

Service: It provides support to implement Windows Services They run in the background and start when the computer starts
Service(void)
static void DisplayLastError(const wchar_t* pszWhere, bool bServiceNotification)
virtual void Run(const wchar_t* pszUserName, const wchar_t* pszPassword, wchar_t* lpCmdLine, LPSERVICE_MAIN_FUNCTION pServiceFunction)
virtual wchar_t* GetDescription()
virtual wchar_t* GetDisplayName()
virtual wchar_t* GetName()
virtual ~ Service(void)
void WINAPI ServiceMain(DWORD dwArgc, PTSTR* pszArgv)

Smtp: It provides support to send email using SMTP in a multithread program
DWORD ThreadFunc(Mt::ThreadLink& threadLink)
Smtp()
const wchar_t* TestConnection(const wchar_t* smtpServerName)
static void GetTimeHeader(char* buffer, int bufferSize)
static void GetTimeHeader(wchar_t* buffer, int bufferSize)
virtual ~ Smtp()
void Setup(const wchar_t* smtpServerName, const wchar_t* username, const wchar_t* password, int port, bool isSSL, DWORD protocol)
For normal SMTP use port 25 For SSL mail such as smtp.gmail.com use port 465 and protocol SP_PROT_TLS1
wchar_t* GetErrorDescr()

SocketLink
SocketLink& operator =(const Mt::SocketLink& init)
SocketLink()
SocketLink(const Mt::SocketLink& init)
bool IsConnected()
bool NetworkEvent(bool readyRead, bool readySend, bool readyConnect, bool isClosing, Mt::StringArray& statusInfo)
bool RecvQueuePop(Mt::SocketPacket& out_socketPacket)
size_t GetRecvQueueSize()
size_t GetSendQueueSize()
void ClearRecvQueue()
void ClearSendQueue()
void Constructor()
void Copy(const Mt::SocketLink& init)
void Delete()
void Disconnect()
void PushInSendQueue(Mt::SocketPacket& socketPacket)
void ResetSend()
~ SocketLink()

SocketManager
SocketManager()
bool RunClient()
It runs the client
bool RunClientX()
It runs the client in another thread
bool RunServer()
It runs the server
bool RunServerX()
It runs the server in another thread
bool Stop()
int GetNumberListenSockets(int port, int protocol)
~ SocketManager()

SocketPacket
SocketPacket()
bool Load(const wchar_t* filename)
bool Save(const wchar_t* filename)
void Delete()
~ SocketPacket()

StringArray: vector Thread Safe
StringArray()
bool empty()
size_t capacity()
size_t max_size()
size_t size()
void Get(vector<wstring >& output)
void GetAndClear(vector<wstring >& output)
void Set(const vector<wstring >& input)
void clear()
void pop_back()
void push_back(const wstring& value)
void reserve(size_t new_cap)
void resize(size_t new_cap)
~ StringArray()

StringTs: wchar_t* Thread Safe
StringTs(void)
bool CopyData(wchar_t* buffer, int bufferSize)
bool Set(const wchar_t* text)
int CreateCopyData(wchar_t* * buffer)
int ExtractCopyData(wchar_t* * buffer)
int GetLength()
~ StringTs(void)

SuspendedThread: It provides support to create a thread that can be suspended. Observe that the same thread can be re-used several times, and thus, reducing the overhead of creating and destroying threads You should not destroy an object of this class if the thread is busy
DWORD WaitForExit()
It waits until the thread is suspended, it returns one of WAIT_ABANDONED, WAIT_OBJECT_0, WAIT_TIMEOUT, WAIT_FAILED  Google Microsoft
HANDLE GetHandle()
Mt::SuspendedThread& operator =(const Mt::SuspendedThread& init)
SuspendedThread()
SuspendedThread(const Mt::SuspendedThread& init)
bool IsBusy()
bool WakeUp()
Call this function to wake up the thread. You need to create a new class derived from Mt::SuspendedThread to call this function and derive ThreadFunc
bool WakeUp(Mt::IThreadX& ithreadX)
Call this function to wake up the thread
operator HANDLE() const
operator const HANDLE() const
virtual DWORD ThreadFunc(Mt::BoolTs& cancel, size_t threadIndex, size_t numThreads)
void Delete()
It sets the event to stop the thread, then it deletes the thread
~ SuspendedThread()

SuspendedThreadPool: It creates a set of suspended threads to perform parallel programming The number of threads is initially set to the number of logical processors You should not destroy an object of this class if any of the threads in the pool is busy
DWORD WaitForAll()
It waits until all the threads in the pool are completed  Google Microsoft
DWORD WaitForOne(size_t threadIndex)
It wait until the specified thread in the pool completes execution.
Mt::SuspendedThread& operator [ ](size_t threadIndex)
Mt::SuspendedThreadPool& operator =(const Mt::SuspendedThreadPool& init)
SuspendedThreadPool()
SuspendedThreadPool(const Mt::SuspendedThreadPool& init)
bool SetNumThreads(size_t numThreads)
bool WakeUpOne(size_t threadIndex, Mt::IThreadX& ithreadX)
It wakes up the specified thread in the pool. It returns true if successful  Google Microsoft
int WaitForOne()
It returns the index of the thread that has completed execution or -1 if error  Google Microsoft
size_t GetNumBusyThreads()
It return the number of threads that are currently busy (running)
size_t GetNumThreads()
size_t WakeUp(Mt::IThreadX& ithreadX, size_t numThreads)
It wakes up a specified number of threads in the pool. It returns the number of threads that woke up
size_t WakeUpAll(Mt::IThreadX& ithreadX)
It wakes up all the threads in the pool. It returns the number of threads that woke up
void Delete()
It sets the event of each thread to stop it, then it deletes all threads in the pool
~ SuspendedThreadPool()
size_t NumThreads

Thread: It provides support to manage a thread. You must not destroy a thread object when IsStillActive returns true
DWORD WaitForExit()
It waits until verify the thread has stopped. it returns one of WAIT_ABANDONED, WAIT_OBJECT_0, WAIT_TIMEOUT, WAIT_FAILED  Google Microsoft
HANDLE GetHandle()
Thread()
bool GetExitCode(DWORD& out_threadExitCode)
It retrieves the termination status of the specified thread This function returns immediately. If the specified thread has not terminated and the function succeeds, the status returned is STILL_ACTIVE. If the thread has terminated and the function succeeds, the status returned is one of the following values:The exit value specified in the ExitThread or TerminateThread function, the return value from the thread function  Google Microsoft
bool IsStillActive()
operator HANDLE() const
operator const HANDLE() const
void Close()
It closes the handle of the thread  Google Microsoft
~ Thread()

ThreadLink
ThreadLink()
bool IsCancel()
bool IsResetTime()
bool IsStopAndContinue()
double GetProgress()
void Reset()
void SetCancel(bool cancel)
void SetProgress(double progress)
void SetResetTime(bool resetTime)
void SetStopAndContinue(bool stopAndContinue)
~ ThreadLink()
bool Cancel
double Progress
bool ResetTime
bool StopAndContinue

ThreadObject: It provides support to create a thread. You must not destroy a thread object when IsStillActive returns true
DWORD WaitForExit()
It waits until verify the thread has stopped. it returns one of WAIT_ABANDONED, WAIT_OBJECT_0, WAIT_TIMEOUT, WAIT_FAILED  Google Microsoft
HANDLE BeginThread()
It creates a thread. If error, it returns zero. Otherwise, it returns a handle to the new thread . You MUST create a new class overriding ThreadObject::ThreadFunc(...)  Google Microsoft
HANDLE BeginThread(Mt::IThread& ithread)
It creates a thread. If error, it returns zero. Otherwise, it returns a handle to the new thread  Google Microsoft
HANDLE GetHandle()
ThreadObject()
bool GetExitCode(DWORD& out_threadExitCode)
It retrieves the termination status of the specified thread This function returns immediately. If the specified thread has not terminated and the function succeeds, the status returned is STILL_ACTIVE. If the thread has terminated and the function succeeds, the status returned is one of the following values:The exit value specified in the ExitThread or TerminateThread function, the return value from the thread function  Google Microsoft
bool IsCancel()
bool IsResetTime()
bool IsStillActive()
bool IsStopAndContinue()
double GetProgress()
double GetProgressInfo(wchar_t* buffer, int buffer_len)
It returns the progress and text information about the progress of the thread
operator HANDLE() const
operator const HANDLE() const
unsigned int GetThreadID() const
void Close()
It closes the handle of the thread  Google Microsoft
void SetCancel(bool cancel)
void SetProgress(double progress)
void SetResetTime(bool resetTime)
void SetStopAndContinue(bool stopAndContinue)
~ ThreadObject()
bool Cancel
double Progress
bool ResetTime
bool StopAndContinue

ThreadObjectX: It provides support to create a thread. You must not destroy a thread object when IsStillActive returns true. This is a light weight thread
DWORD WaitForExit()
It waits until verify the thread has stopped. it returns one of WAIT_ABANDONED, WAIT_OBJECT_0, WAIT_TIMEOUT, WAIT_FAILED  Google Microsoft
HANDLE BeginThread()
It creates a thread. If error, it returns zero. Otherwise, it returns a handle to the new thread You MUST create a new class overriding ThreadObjectX::ThreadFunc(...)  Google Microsoft
HANDLE BeginThread(Mt::IThreadX& ithreadX)
It creates a thread. If error, it returns zero. Otherwise, it returns a handle to the new thread  Google Microsoft
HANDLE GetHandle()
ThreadObjectX()
bool GetExitCode(DWORD& out_threadExitCode)
It retrieves the termination status of the specified thread This function returns immediately. If the specified thread has not terminated and the function succeeds, the status returned is STILL_ACTIVE. If the thread has terminated and the function succeeds, the status returned is one of the following values:The exit value specified in the ExitThread or TerminateThread function, the return value from the thread function  Google Microsoft
bool IsStillActive()
operator HANDLE() const
operator const HANDLE() const
unsigned int GetThreadID() const
void Close()
It closes the handle of the thread  Google Microsoft
~ ThreadObjectX()

ThreadPool: It creates a set of threads to perform parallel programming The number of threads is initially set to the number of logical processors You should not destroy an object of this class, if any of the threads in the pool is busy
DWORD WaitForAll()
It waits until all the threads in the pool are completed. It returns: WAIT_TIMEOUT, WAIT_FAILED,...  Google Microsoft
DWORD WaitForOne(size_t threadIndex)
It wait until the specified thread in the pool completes execution.
Mt::ThreadObjectX& operator [ ](size_t threadIndex)
Mt::ThreadPool& operator =(const Mt::ThreadPool& init)
ThreadPool()
ThreadPool(const Mt::ThreadPool& init)
bool AreAllThreadsStopped()
bool BeginOne(size_t threadIndex, Mt::IThreadX& ithreadX)
It begins the execution of the specified thread in the pool. It returns true if successful  Google Microsoft
bool Reset()
It destroys the threads in the pool and creates a new set of threads
bool SetNumThreads(size_t numThreads)
int WaitForOne()
It returns the thread index or -1 if error
size_t Begin(Mt::IThreadX& ithreadX, size_t numThreads)
It begins the execution of specified number of threads in the pool. It returns the number of threads that started if error  Google Microsoft
size_t BeginAll(Mt::IThreadX& ithreadX)
It begins the execution of all the threads in the pool. It returns the number of threads that startedif error  Google Microsoft
size_t GetNumActiveThreads()
It return the number of threads that are active (running)
size_t GetNumThreads()
void Cancel()
It set the cancel variable of each thread to true
void Delete()
It deletes all threads in the pool
~ ThreadPool()
size_t NumThreads

WstringTs: wstring Thread Safe
WstringTs(void)
bool ConcatTry(const wstring& text)
bool GetAndClearTry(wstring& out_text)
bool GetTry(wstring& out_text)
bool SetTry(const wstring& text)
void Clear()
void Concat(const wstring& text)
void Get(wstring& out_text)
void GetAndClear(wstring& out_text)
void Set(const wstring& text)
~ WstringTs(void)
© Copyright 2000-2021 selo. All Rights Reserved. Jul 22 2021.